package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.exception.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.cache.AlbumCacheRepository;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SKUMapper;
import cn.tedu.csmall.product.mapper.SPUMapper;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Picture;
import cn.tedu.csmall.product.pojo.entity.SKU;
import cn.tedu.csmall.product.pojo.entity.SPU;
import cn.tedu.csmall.product.pojo.param.AlbumInsertParam;
import cn.tedu.csmall.product.pojo.param.AlbumUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AlbumListVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.commons.util.PageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    AlbumMapper mapper;
    @Autowired
    PictureMapper pictureMapper;
    @Autowired
    SKUMapper skuMapper;
    @Autowired
    SPUMapper spuMapper;
    @Override
    public void insert(AlbumInsertParam albumInsertParam) {
        log.debug("开始处理【添加相册】的业务，参数：{}", albumInsertParam);
        QueryWrapper<Album> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",albumInsertParam.getName());
        int countByName = mapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByName);
        if(mapper.selectCount(queryWrapper)>0){
            String message="添加相册失败，相册名称已被占用!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_CONFLICT);
        }


        Album album=new Album();
        BeanUtils.copyProperties(albumInsertParam,album);
        album.setGmtCreate(LocalDateTime.now());
        album.setGmtModified(LocalDateTime.now());
        log.debug("准备将新的相册数据写入到数据库，数据：{}", album);
        int row = mapper.insert(album);
        if(row !=1){
            String message="添加管理员失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_INSERT);
        }
        log.debug("将新的相册数据写入到数据库，完成！");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据ID删除相册】的业务，参数：{}", id);
        // 检查相册是否存在，如果不存在，则抛出异常
        QueryWrapper<Album> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", id);
        int countById = mapper.selectCount(queryWrapper1);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}", countById);
        if (countById == 0) {
            String message = "删除相册失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(message,ServiceCode.ERR_NOT_FOUND);
        }
        //检查是否有图片关联此相册
        QueryWrapper<Picture> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("album_id",id);
        int countByAlbumId = pictureMapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByAlbumId);
        if(pictureMapper.selectCount(queryWrapper)>0){
            String message="删除相册失败，仍有图片关联此相册!";
            log.warn(message);
            throw new ServiceException(message,ServiceCode.ERR_CONFLICT);
        }
        //检查是否有SKU关联此相册
        QueryWrapper<SKU> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("album_id",id);
        int countByAlbumId2 = skuMapper.selectCount(queryWrapper2);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByAlbumId2);
        if(countByAlbumId2>0){
            String message="删除相册失败，仍有sku关联此相册!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_CONFLICT);
        }
        //检查是否有SPU关联此相册
        QueryWrapper<SPU> queryWrapper3=new QueryWrapper<>();
        queryWrapper3.eq("album_id",id);
        int countByAlbumId3 = spuMapper.selectCount(queryWrapper3);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByAlbumId3);
        if(countByAlbumId3>0){
            String message="删除相册失败，仍有spu关联此相册!";
            log.warn(message);
            throw new ServiceException(message,ServiceCode.ERR_CONFLICT);
        }

        int row = mapper.deleteById(id);
        if(row !=1){
            String message="添加管理员失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException( message,ServiceCode.ERR_DELETE);
        }
        log.debug("根据ID从数据库中删除，完成！");
    }

    @Override
    public void update(AlbumUpdateInfoParam albumUpdateInfoParam) {
        log.debug("开始处理【修改相册】的业务，参数：{}", albumUpdateInfoParam);
        QueryWrapper<Album> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("id", albumUpdateInfoParam.getId());
        int countById = mapper.selectCount(queryWrapper1);
        log.debug("根据相册ID统计匹配的相册数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改相册失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(message,ServiceCode.ERR_NOT_FOUND);
        }

        QueryWrapper<Album> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",albumUpdateInfoParam.getName())
        .ne("id",albumUpdateInfoParam.getId());
        int countByName = mapper.selectCount(queryWrapper);
        log.debug("根据相册名称统计匹配的相册数量，结果：{}", countByName);
        if(mapper.selectCount(queryWrapper)>0){
            String message="修改相册失败，相册名称已被占用!";
            log.warn(message);
            throw new ServiceException(message,ServiceCode.ERR_CONFLICT);
        }
        //执行修改
        Album album=new Album();
        BeanUtils.copyProperties(albumUpdateInfoParam,album);
        int row = mapper.updateById(album);
        if(row !=1){
            String message="添加管理员失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(message, ServiceCode.ERR_UPDATE);
        }
        log.debug("根据ID从数据库中修改，完成！");
    }

    @Autowired
    AlbumCacheRepository albumCacheRepository;
    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册】的业务，参数：{}", id);
        AlbumStandardVO result = albumCacheRepository.getStandardById(id);
        if(result==null){
            String message = "查询相册详情失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException( message,ServiceCode.ERR_NOT_FOUND);
        }
        return result;
    }

    @Override
    public PageData<AlbumListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询相册】的业务，页数：{}，每页的数据量：{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<AlbumListVO> list=mapper.list();
        PageInfo<AlbumListVO> pageInfo=new PageInfo<>(list);
        PageData<AlbumListVO> pageData= PageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<AlbumListVO> list(Integer pageNum) {
        Integer pageSize=5;
        return list(pageNum,pageSize);
    }


}
